/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.managedBeans.gestionVehicule;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.CvAdministratifvehiculeJpaController;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.CvMarqueJpaController;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.CvModelJpaController;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.CvTypemodelJpaController;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.CvVehiculeJpaController;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.PreexistingEntityException;
import fr.cggveritas.crewintranet.jms.entities.CvAdministratifvehicule;
import fr.cggveritas.crewintranet.jms.entities.CvMarque;
import fr.cggveritas.crewintranet.jms.entities.CvModel;
import fr.cggveritas.crewintranet.jms.entities.CvTypemodel;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import fr.cggveritas.crewintranet.jms.managedBeans.utils.RedirectionPage;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

/**
 *
 * @author Alassane
 */
@ManagedBean(name = "vehiculeCreerBean")
@SessionScoped
public class VehiculeCreerBean implements Serializable {

    /**
     * Creates a new instance of VehiculeBean
     */
    private CvVehiculeJpaController cvVehiculeJpaController;
    private CvVehicule cvVehiculeSelectionner;
    private CvVehicule cvVehiculeNew;
    private CvVehicule cvVehiculeEdition;
    private List<CvVehicule> filterListCvVehicules;
    private List<String> listeCategorieVehicule;
    private List<String> listeCeinture;
    private CvMarqueJpaController cvMarqueJpaController;
    private CvModelJpaController cvModelJpaController;
    private CvTypemodelJpaController cvTypemodelJpaController;
    private CvMarque cvMarque;
    private List<CvMarque> listCvmarques;
    private CvModel cvModel;
    private List<CvModel> listCvModels;
    private CvTypemodel cvTypemodel;
    private List<CvTypemodel> listCvTypemodels;
    private List<CvModel> modelListeHand;
    private List<CvTypemodel> typemodelListeHand;
    private CvAdministratifvehiculeJpaController cvAdministratifvehiculeJpaController;
    private CvAdministratifvehicule cvAdministratifvehicule;
    private StringBuilder pagePrecedente;
    
    public VehiculeCreerBean() {
        cvVehiculeJpaController = new CvVehiculeJpaController();
        cvVehiculeSelectionner = new CvVehicule();
        cvVehiculeNew = new CvVehicule();
        filterListCvVehicules = new ArrayList<CvVehicule>();
        listeCategorieVehicule = new ArrayList<String>();
        listeCeinture = new ArrayList<String>();
        listeCategorieVehicule.add("LV");
        listeCategorieVehicule.add("PL");

        listeCeinture.add("Oui");
        listeCeinture.add("2 points");
        listeCeinture.add("3 points");
        listeCeinture.add("4 points");

        cvMarqueJpaController = new CvMarqueJpaController();
        cvModelJpaController = new CvModelJpaController();
        cvTypemodelJpaController = new CvTypemodelJpaController();

        cvMarque = new CvMarque();
        cvModel = new CvModel();
        cvTypemodel = new CvTypemodel();
        listCvmarques = new ArrayList<CvMarque>();
        listCvModels = new ArrayList<CvModel>();
        modelListeHand = new ArrayList<CvModel>();
        typemodelListeHand = new ArrayList<CvTypemodel>();
        listCvTypemodels = new ArrayList<CvTypemodel>();

        cvAdministratifvehiculeJpaController = new CvAdministratifvehiculeJpaController();
        cvAdministratifvehicule = new CvAdministratifvehicule();
        pagePrecedente = new StringBuilder("");
    }

    public String creerUnVehicule() {
        initialisation();
        return RedirectionPage.redirectionCreerVehicule;
    }

    public String retourDansListe() {
        initialisation();
        return RedirectionPage.redirectionAccueilVehicule;
    }

    public String enregistrerVehicule() throws IllegalOrphanException, PreexistingEntityException, Exception {

        if (cvTypemodel != null) {
            cvVehiculeNew.setTypemodel(cvTypemodel);
            cvVehiculeNew.setCvModelIdmodel(cvTypemodel.getCvModelIdmodel());
            cvVehiculeNew.setCvMarqueIdmarque(cvTypemodel.getCvModelIdmodel().getCvMarqueIdmarque());
        }
        cvVehiculeJpaController.create(cvVehiculeNew);
        cvAdministratifvehicule.setIdvehicule(cvVehiculeNew.getIdvehicule());
        cvAdministratifvehicule.setCvVehicule(cvVehiculeNew);
        cvAdministratifvehiculeJpaController.create(cvAdministratifvehicule);

        initialisation();
        return RedirectionPage.redirectionAccueilVehicule;
    }

    public String voirDetailDunVehicule() {
        if (cvVehiculeSelectionner == null || cvVehiculeSelectionner.getIdvehicule() == null) {
            return RedirectionPage.redirectionAccueilVehicule;
        }
        
        return RedirectionPage.redirectionDetailVehicule;
    }
    public String voirDetailDunVehiculeView() {
               
        return RedirectionPage.redirectionDetailVehiculeView;
    }

    public void handleMarqueChange() {
        modelListeHand = new ArrayList<CvModel>();
        typemodelListeHand = new ArrayList<CvTypemodel>();
        cvTypemodel = new CvTypemodel();
        if (cvMarque != null) {
            modelListeHand = cvModelJpaController.findByMarque(cvMarque);
        } else {
            modelListeHand = new ArrayList<CvModel>();
            cvModel = new CvModel();
        }

    }

    public void handleModeleChange() {
        typemodelListeHand = new ArrayList<CvTypemodel>();

        if (cvModel != null) {
            typemodelListeHand = cvTypemodelJpaController.findByModel(cvModel);
        } else {
            typemodelListeHand = new ArrayList<CvTypemodel>();
            cvTypemodel = new CvTypemodel();
        }

    }

    public List<CvVehicule> getListeVehicule() {
        return cvVehiculeJpaController.findCvVehiculeEntities();
    }

    public CvVehiculeJpaController getCvVehiculeJpaController() {
        return cvVehiculeJpaController;
    }

    public void setCvVehiculeJpaController(CvVehiculeJpaController cvVehiculeJpaController) {
        this.cvVehiculeJpaController = cvVehiculeJpaController;
    }

    public CvVehicule getCvVehiculeSelectionner() {
        return cvVehiculeSelectionner;
    }

    public void setCvVehiculeSelectionner(CvVehicule cvVehiculeSelectionner) {
        this.cvVehiculeSelectionner = cvVehiculeSelectionner;
    }

    public List<CvVehicule> getFilterListCvVehicules() {
        return filterListCvVehicules;
    }

    public void setFilterListCvVehicules(List<CvVehicule> filterListCvVehicules) {
        this.filterListCvVehicules = filterListCvVehicules;
    }

    public void initialisation() {
        cvVehiculeNew = new CvVehicule();
        cvAdministratifvehicule = new CvAdministratifvehicule();
        cvTypemodel = new CvTypemodel();
        cvMarque = new CvMarque();
        cvModel = new CvModel();
        modelListeHand = new ArrayList<CvModel>();
        typemodelListeHand = new ArrayList<CvTypemodel>();
    }

    public CvVehicule getCvVehiculeNew() {
        return cvVehiculeNew;
    }

    public void setCvVehiculeNew(CvVehicule cvVehiculeNew) {
        this.cvVehiculeNew = cvVehiculeNew;
    }

    public List<String> getListeCategorieVehicule() {
        return listeCategorieVehicule;
    }

    public void setListeCategorieVehicule(List<String> listeCategorieVehicule) {
        this.listeCategorieVehicule = listeCategorieVehicule;
    }

    public CvMarqueJpaController getCvMarqueJpaController() {
        return cvMarqueJpaController;
    }

    public void setCvMarqueJpaController(CvMarqueJpaController cvMarqueJpaController) {
        this.cvMarqueJpaController = cvMarqueJpaController;
    }

    public CvModelJpaController getCvModelJpaController() {
        return cvModelJpaController;
    }

    public void setCvModelJpaController(CvModelJpaController cvModelJpaController) {
        this.cvModelJpaController = cvModelJpaController;
    }

    public CvTypemodelJpaController getCvTypemodelJpaController() {
        return cvTypemodelJpaController;
    }

    public void setCvTypemodelJpaController(CvTypemodelJpaController cvTypemodelJpaController) {
        this.cvTypemodelJpaController = cvTypemodelJpaController;
    }

    public CvMarque getCvMarque() {
        return cvMarque;
    }

    public void setCvMarque(CvMarque cvMarque) {
        this.cvMarque = cvMarque;
    }

    public List<CvMarque> getListCvmarques() {
        return listCvmarques;
    }

    public void setListCvmarques(List<CvMarque> listCvmarques) {
        this.listCvmarques = listCvmarques;
    }

    public CvModel getCvModel() {
        return cvModel;
    }

    public void setCvModel(CvModel cvModel) {
        this.cvModel = cvModel;
    }

    public List<CvModel> getListCvModels() {
        return listCvModels;
    }

    public void setListCvModels(List<CvModel> listCvModels) {
        this.listCvModels = listCvModels;
    }

    public CvTypemodel getCvTypemodel() {
        return cvTypemodel;
    }

    public void setCvTypemodel(CvTypemodel cvTypemodel) {
        this.cvTypemodel = cvTypemodel;
    }

    public List<CvTypemodel> getListCvTypemodels() {
        return listCvTypemodels;
    }

    public void setListCvTypemodels(List<CvTypemodel> listCvTypemodels) {
        this.listCvTypemodels = listCvTypemodels;
    }

    public List<CvModel> getModelListeHand() {
        return modelListeHand;
    }

    public void setModelListeHand(List<CvModel> modelListeHand) {
        this.modelListeHand = modelListeHand;
    }

    public List<CvTypemodel> getTypemodelListeHand() {
        return typemodelListeHand;
    }

    public void setTypemodelListeHand(List<CvTypemodel> typemodelListeHand) {
        this.typemodelListeHand = typemodelListeHand;
    }

    public List<String> getListeCeinture() {
        return listeCeinture;
    }

    public void setListeCeinture(List<String> listeCeinture) {
        this.listeCeinture = listeCeinture;
    }

    public CvAdministratifvehiculeJpaController getCvAdministratifvehiculeJpaController() {
        return cvAdministratifvehiculeJpaController;
    }

    public void setCvAdministratifvehiculeJpaController(CvAdministratifvehiculeJpaController cvAdministratifvehiculeJpaController) {
        this.cvAdministratifvehiculeJpaController = cvAdministratifvehiculeJpaController;
    }

    public CvAdministratifvehicule getCvAdministratifvehicule() {
        return cvAdministratifvehicule;
    }

    public void setCvAdministratifvehicule(CvAdministratifvehicule cvAdministratifvehicule) {
        this.cvAdministratifvehicule = cvAdministratifvehicule;
    }

    public CvVehicule getCvVehiculeEdition() {
        return cvVehiculeEdition;
    }

    public void setCvVehiculeEdition(CvVehicule cvVehiculeEdition) {
        this.cvVehiculeEdition = cvVehiculeEdition;
    }

    public String getPagePrecedente() {
        return this.pagePrecedente.toString();
    }

    public void setPagePrecedente(String pagePrecedente) {
        this.pagePrecedente = new StringBuilder(pagePrecedente);
    }
    
    
    
}
